package com.leetcode.partition1;

/**
 * @author `RKC`
 * @date 2021/9/21 9:09
 */
public class LC58最后一个单词的长度 {

    public static int lengthOfLastWord(String s) {
//        String[] arr = s.split(" ");
//        System.out.println(Arrays.toString(arr));
//        return arr[arr.length - 1].length();
        char[] words = s.toCharArray();
        int answer = 0;
        boolean flag = false;
        for (int i = words.length - 1; i >= 0; i--) {
            if (words[i] == ' ' && flag) break;
            if (words[i] == ' ') continue;
            flag = true;
            answer++;
        }
        return answer;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLastWord(" a  "));
    }
}
